'''
Created on 27/apr/09

@author: Riccardo Cagnasso
'''
from Datastore.TicketDB import TicketDB
import os
from google.appengine.ext.webapp import template

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from Datastore.Ticket.Ticket import Ticket
from Datastore.Ticket.Entry import Entry


class TicketList(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        tickets = Ticket.all()
        
        template_vars={
                        'tickets':tickets
                       }
                #self.response.out.write( entry.key() )
                
        path = os.path.join(os.path.dirname(__file__), 'templates\\listTickets.html')
        self.response.out.write(template.render(path, template_vars))
                
                
    
                        
class AddTicket(webapp.RequestHandler):
    def post(self):
        state = self.request.get('state')
        priority = self.request.get('priority')
        content = self.request.get('content')
        
        Ticket(
               state=state,
               priority=priority,
               content=content
               ).put()
               
        self.redirect("/",True)

class WriteTestData(webapp.RequestHandler):
    def get(self):
        ticket = Ticket(state="open")
        ticket.content="foo"
        ticket.put()
        
        entry = Entry()
        entry.id=1
        entry.ticket=ticket
        entry.content="bar"
        entry.put()
        
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write("Test data written")

application = webapp.WSGIApplication(
                                     [
                                      ('/', TicketList),
                                      ('/addTicket',AddTicket),
                                      ('/test',WriteTestData)
                                      ],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
